/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Common;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Quang Nhat
 */
public class Utils {

    public static Connection makeConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        return DriverManager.getConnection("jdbc:sqlserver://localhost\\sqlexpress:1433;databasename=Accounting;user=sa;password=TopWin266");
    }

    public static String getCheckSum(String message) {
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            BigInteger i = new BigInteger(1, md.digest(message.getBytes()));
            return String.format("%1$032x", i).toUpperCase();
        } catch (Exception ex) {
            ReportException(ex);
        }
        return null;
    }

    public static void ReportException(Exception ex) {
        ex.printStackTrace();
    }

    public static <T> void Marshall(T obj, OutputStream out, boolean format) {
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller mar = context.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
            mar.marshal(obj, out);
        } catch (JAXBException ex) {
            ReportException(ex);
        }
    }

    public static <T> byte[] Marshall(T obj, boolean format) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Marshall(obj, out, format);
        return out.toByteArray();
    }

    public static <T> String MarshallXML(T obj, boolean format) {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Marshall(obj, out, format);
            return out.toString("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            ReportException(ex);
        }
        return null;
    }

    public static Date parseDate(String txt) {
        if (txt == null) {
            return null;
        }
        String[] parts = txt.split("/");
        if (parts.length != 3) {
            return null;
        }
        try {
            int year = Integer.parseInt(parts[2]);
            int month = Integer.parseInt(parts[1]);
            int day = Integer.parseInt(parts[0]);
            Date res = new Date(year - 1900, month - 1, day);
            if (year <= 0 || month <= 0 || day <= 0) {
                return null;
            }
            return res;
        } catch (Exception ex) {
        }
        return null;
    }
}
